﻿using AnesSystem.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AnesSystem.Controls
{
    /// <summary>
    /// DrugLineItemCtrl.xaml 的交互逻辑
    /// </summary>
    public partial class DrugLineBoxCtrl : UserControl
    {

        #region Variables
        private int _currentPage = 1;

        private List<PatientDrugsModel> _patientDruglist = new List<PatientDrugsModel>();

        private Dictionary<DrugSpeedModel, DrugLineEnumCtrl> _drugLineItemMap = new Dictionary<DrugSpeedModel, DrugLineEnumCtrl>();

        private Dictionary<DrugSpeedModel, DrugLineEnumCtrl> _drugLinePacuItemMap = new Dictionary<DrugSpeedModel, DrugLineEnumCtrl>();

        #endregion

        #region Constructor

        public DrugLineBoxCtrl()
        {
            InitializeComponent();
            //_gridSpeed.Height = (LocationUtil.Medicine_Height - 2) * 0.6;
            _gridLine.Height = (LocationUtil.Medicine_Height - 2);
            this.Loaded += DrugLineBoxCtrl_Loaded;
        }

        #endregion

        #region Properties

        //public int Index { get; set; }

        public int CurrentPage
        {
            get
            {
                return _currentPage;
            }

            set
            {
                _currentPage = value;
            }
        }

        public Dictionary<DrugSpeedModel, DrugLineEnumCtrl> DrugLineItemMap
        {
            get
            {
                return _drugLineItemMap;
            }

            set
            {
                _drugLineItemMap = value;
            }
        }

        public List<PatientDrugsModel> PatientDruglist
        {
            get
            {
                return _patientDruglist;
            }

            set
            {
                _patientDruglist = value;
            }
        }

        public Dictionary<DrugSpeedModel, DrugLineEnumCtrl> DrugLinePacuItemMap
        {
            get
            {
                return _drugLinePacuItemMap;
            }

            set
            {
                _drugLinePacuItemMap = value;
            }
        }


        #endregion

        #region 依赖属性

        #endregion

        #region Event

        #endregion

        #region Private Methods

        #endregion

        #region Public/Protected Methods

        #endregion

        #region EventHandlers

        private void DrugLineBoxCtrl_Loaded(object sender, RoutedEventArgs e)
        {
            //DrawLine();
        }
        public void DrawLine()
        {
            DrugLineItemMap.Clear();
            PatientDruglist = this.DataContext as List<PatientDrugsModel>;
            foreach (var patientDrug in PatientDruglist)
            {
                if (patientDrug.Single)
                {
                    _gridSingle.Visibility = Visibility.Visible;
                    _gridContinue.Visibility = Visibility.Collapsed;
                    DrawSingleLineByType(patientDrug);
                }
                else
                {
                    _gridSingle.Visibility = Visibility.Collapsed;
                    _gridContinue.Visibility = Visibility.Visible;
                    //if (patientDrug.EndTime != new DateTime() && patientDrug.EndTime.CompareTo(pageStartTime) < 0) continue;
                    string colorCode = "";
                    //药只有三种状态不会继续在页面画线：1、点击结束按钮；2、病人出室；3、暂停；
                    //用药已经结束，分1,2两种状态，页面显示黑色的线

                    //药点击了结束按钮，已结束
                    //病人已经出室，已结束     
                    if (Constants.ACTION_FINISH.Equals(patientDrug.Status) ||
                        DataManager.Instance.CurrentPatientLink.Status.OutRoomTime != new DateTime()) colorCode = "black";
                    else colorCode = "red";//药未结束，还会在向后画线，页面显示红色的线                
                    DrawLineByType(colorCode, patientDrug);
                }
            }
        }
        public void DrawPacuLine()
        {
            DrugLinePacuItemMap.Clear();
            PatientDruglist = this.DataContext as List<PatientDrugsModel>;
            foreach (var patientDrug in PatientDruglist)
            {
                if (patientDrug.Single)
                {
                    _gridSingle.Visibility = Visibility.Visible;
                    _gridContinue.Visibility = Visibility.Collapsed;
                    DrawPacuSingleLineByType(patientDrug);
                }
                else
                {
                    _gridSingle.Visibility = Visibility.Collapsed;
                    _gridContinue.Visibility = Visibility.Visible;
                    //if (patientDrug.EndTime != new DateTime() && patientDrug.EndTime.CompareTo(pageStartTime) < 0) continue;
                    string colorCode = "";
                    //药只有三种状态不会继续在页面画线：1、点击结束按钮；2、病人出室；3、暂停；
                    //用药已经结束，分1,2两种状态，页面显示黑色的线

                    //药点击了结束按钮，已结束
                    //病人已经出室，已结束     
                    if (Constants.ACTION_FINISH.Equals(patientDrug.Status) ||
                        DataManager.Instance.CurrentPatientLink.Status.OutPacuTime != new DateTime()) colorCode = "black";
                    else colorCode = "red";//药未结束，还会在向后画线，页面显示红色的线                
                    DrawLineByPacuType(colorCode, patientDrug);
                }
            }
        }
        public void DrawSingleLineByType(PatientDrugsModel patientDrug)
        {
            DateTime pageStartTime = GetPageStartTime();
            DateTime pageEndTime = GetPageEndTime();
            DateTime nowTime = DateTime.Now;
            List<DrugSpeedModel> listDrugSpeed = patientDrug.DrugSpeeds.OrderBy(O => O.BeginTime).ToList();
            for (int i = 0; i < listDrugSpeed.Count(); i++)
            {
                DrawDrugLineModel lineEnum = DrawDrugLineModel.VerticalBlackLineNoRight;
                DrugLineEnumCtrl ctrl = new DrugLineEnumCtrl();
                ctrl.CurrentPage = CurrentPage;
                ctrl.SingleDisplay(lineEnum, patientDrug, listDrugSpeed[i]);
                _gridSingleLine.Children.Add(ctrl);
            }
        }
        public void DrawPacuSingleLineByType(PatientDrugsModel patientDrug)
        {
            DateTime pageStartTime = GetPageStartTime();
            DateTime pageEndTime = GetPageEndTime();
            DateTime nowTime = DateTime.Now;
            List<DrugSpeedModel> listDrugSpeed = patientDrug.DrugSpeeds.OrderBy(O => O.BeginTime).ToList();
            for (int i = 0; i < listDrugSpeed.Count(); i++)
            {
                DrawDrugLineModel lineEnum = DrawDrugLineModel.VerticalBlackLineNoRight;
                DrugLineEnumCtrl ctrl = new DrugLineEnumCtrl();
                ctrl.CurrentPage = CurrentPage;
                ctrl.PacuSingleDisplay(lineEnum, patientDrug, listDrugSpeed[i]);
                _gridSingleLine.Children.Add(ctrl);
            }
        }
        public void DrawLineByType(string colorCode, PatientDrugsModel patientDrug)
        {
            int p = CurrentPage;
            DateTime pageStartTime = GetPageStartTime();
            DateTime pageEndTime = GetPageEndTime();
            DateTime nowTime = DateTime.Now;
            List<DrugSpeedModel> listDrugSpeed = patientDrug.DrugSpeeds.OrderBy(O => O.BeginTime).ToList();
            for (int i = 1; i <= listDrugSpeed.Count(); i++)
            {
                DrawDrugLineModel lineEnum = new DrawDrugLineModel();
                DrugLineEnumCtrl ctrl = new DrugLineEnumCtrl();
                DrugSpeedModel dsm1 = new DrugSpeedModel();
                DrugSpeedModel dsm2 = null;
                ctrl.StrokeColor = "black".Equals(colorCode) ? Brushes.Black : Brushes.Red;
                ctrl.CurrentPage = CurrentPage;
                if (i > listDrugSpeed.Count() - 1)
                {
                    //如果下一个为空              
                    if (listDrugSpeed[i - 1].BeginTime.CompareTo(pageEndTime) > 0 ||
                        Constants.ACTION_PAUSE.Equals(listDrugSpeed[i - 1].Actions)) continue;
                    if (Constants.ACTION_FINISH.Equals(patientDrug.Status) || DataManager.Instance.CurrentPatientLink.Status.OutRoomTime != new DateTime())
                    {
                        if (patientDrug.EndTime == new DateTime())
                        {
                            if (DataManager.Instance.CurrentPatientLink.Status.OutRoomTime.CompareTo(pageEndTime) < 0)
                            {
                                lineEnum = DrawDrugLineModel.VerticalBlackLine;
                            }
                            else
                            {
                                lineEnum = DrawDrugLineModel.VerticalLine;
                            }
                        }
                        else
                        {
                            if (patientDrug.EndTime.CompareTo(pageEndTime) < 0)
                            {
                                if (patientDrug.TakeOut)
                                {
                                    lineEnum = DrawDrugLineModel.VerticalBlackObliqueLine;
                                }
                                else
                                {
                                    lineEnum = DrawDrugLineModel.VerticalBlackLine;
                                }
                            }
                            else
                            {
                                lineEnum = DrawDrugLineModel.VerticalLine;
                            }
                        }
                    }
                    else
                    {
                        if (nowTime.CompareTo(pageEndTime) > 0)
                        {
                            lineEnum = DrawDrugLineModel.VerticalLine;
                        }
                        else
                        {
                            lineEnum = DrawDrugLineModel.VerticalArrowLine;
                        }
                    }
                }
                else
                {
                    //不是最后一个
                    //如果当前Item比页面开始事件还小，说明不是这一页的线，跳过；当前item的Action为暂停，跳过
                    if (listDrugSpeed[i].BeginTime.CompareTo(pageStartTime) < 0 ||Constants.ACTION_PAUSE.Equals(listDrugSpeed[i - 1].Actions)) continue;
                    if (listDrugSpeed[i].BeginTime.CompareTo(pageEndTime) > 0)
                    {
                        if (listDrugSpeed[i - 1].BeginTime.CompareTo(pageEndTime) < 0)
                        {
                            lineEnum = DrawDrugLineModel.VerticalLine;
                        }
                        else
                        {
                            continue;
                        }
                    }

                    if (Constants.ACTION_PAUSE.Equals(listDrugSpeed[i].Actions))
                    {
                        lineEnum = DrawDrugLineModel.VerticalTwoLine;
                    }
                    else
                    {
                        lineEnum = DrawDrugLineModel.VerticalLine;
                    }

                    //前面已经判断是否是最后一个，所以这里一定有下一个
                    dsm2 = listDrugSpeed[i];
                }


                //try
                //{
                //    //不是最后一个
                //    //如果当前Item比页面开始事件还小，说明不是这一页的线，跳过；当前item的Action为暂停，跳过
                //    if (listDrugSpeed[i].BeginTime.CompareTo(pageStartTime) < 0 ||
                //        listDrugSpeed[i].BeginTime.CompareTo(pageEndTime) > 0 ||
                //        Constants.ACTION_PAUSE.Equals(listDrugSpeed[i - 1].Actions)) continue;
                //    if (Constants.ACTION_PAUSE.Equals(listDrugSpeed[i].Actions))
                //    {
                //        lineEnum = DrawDrugLineModel.VerticalTwoLine;
                //    }
                //    else
                //    {
                //        lineEnum = DrawDrugLineModel.VerticalLine;
                //    }

                //    //前面已经判断是否是最后一个，所以这里一定有下一个
                //    dsm2 = listDrugSpeed[i];
                //}
                //catch (Exception)
                //{
                //    //如果下一个为空              
                //    if (listDrugSpeed[i - 1].BeginTime.CompareTo(pageEndTime) > 0 ||
                //        Constants.ACTION_PAUSE.Equals(listDrugSpeed[i - 1].Actions)) continue;
                //    if (Constants.ACTION_FINISH.Equals(patientDrug.Status) || DataManager.Instance.CurrentPatientLink.Status.OutRoomTime.Year > 1970)
                //    {
                //        if (patientDrug.EndTime.CompareTo(pageEndTime) > 0 || DataManager.Instance.CurrentPatientLink.Status.OutRoomTime.CompareTo(pageEndTime) > 0)
                //        {
                //            lineEnum = DrawDrugLineModel.VerticalLine;
                //        }
                //        else
                //        {
                //            if (patientDrug.TakeOut)
                //            {
                //                lineEnum = DrawDrugLineModel.VerticalBlackObliqueLine;
                //            }
                //            else
                //            {
                //                lineEnum = DrawDrugLineModel.VerticalBlackLine;
                //            }
                //        }
                //    }
                //    else
                //    {
                //        if (nowTime.CompareTo(pageEndTime) > 0)
                //        {
                //            lineEnum = DrawDrugLineModel.VerticalLine;
                //        }
                //        else
                //        {
                //            lineEnum = DrawDrugLineModel.VerticalArrowLine;
                //        }
                //    }
                //}
                dsm1 = listDrugSpeed[i - 1];
                ctrl.Display(lineEnum, patientDrug, dsm1, dsm2);
                _gridLine.Children.Add(ctrl);
                //DrugLineItemMap.Add(dsm1, ctrl);
                if (i == listDrugSpeed.Count() && !Constants.ACTION_FINISH.Equals(patientDrug.Status))
                {
                    DrugLineItemMap.Add(listDrugSpeed[i - 1], ctrl);
                    //if (dsm2 == null)
                    //{
                    //    DrugLineItemMap.Add(dsm1, ctrl);
                    //}
                    //else
                    //{
                    //    DrugLineItemMap.Add(dsm2, ctrl);
                    //}
                }
            }
        }

        public void DrawLineByPacuType(string colorCode, PatientDrugsModel patientDrug)
        {
            DateTime pageStartTime = GetPacuPageStartTime();
            DateTime pageEndTime = GetPacuPageEndTime();
            DateTime nowTime = DateTime.Now;
            List<DrugSpeedModel> listDrugSpeed = patientDrug.DrugSpeeds.OrderBy(O => O.BeginTime).ToList();
            for (int i = 1; i <= listDrugSpeed.Count(); i++)
            {
                DrawDrugLineModel lineEnum = new DrawDrugLineModel();
                DrugLineEnumCtrl ctrl = new DrugLineEnumCtrl();
                DrugSpeedModel dsm1 = new DrugSpeedModel();
                DrugSpeedModel dsm2 = null;
                ctrl.StrokeColor = "black".Equals(colorCode) ? Brushes.Black : Brushes.Red;
                ctrl.CurrentPage = CurrentPage;
                if (i > listDrugSpeed.Count() - 1)
                {
                    //如果下一个为空              
                    if (listDrugSpeed[i - 1].BeginTime.CompareTo(pageEndTime) > 0 ||
                        Constants.ACTION_PAUSE.Equals(listDrugSpeed[i - 1].Actions)) continue;
                    if (Constants.ACTION_FINISH.Equals(patientDrug.Status) || DataManager.Instance.CurrentPatientLink.Status.OutPacuTime != new DateTime())
                    {
                        if (patientDrug.EndTime == new DateTime())
                        {
                            if (DataManager.Instance.CurrentPatientLink.Status.OutRoomTime.CompareTo(pageEndTime) < 0)
                            {
                                lineEnum = DrawDrugLineModel.VerticalBlackLine;
                            }
                            else
                            {
                                lineEnum = DrawDrugLineModel.VerticalLine;
                            }
                        }
                        else
                        {
                            if (patientDrug.EndTime.CompareTo(pageEndTime) < 0)
                            {
                                if (patientDrug.TakeOut)
                                {
                                    lineEnum = DrawDrugLineModel.VerticalBlackObliqueLine;
                                }
                                else
                                {
                                    lineEnum = DrawDrugLineModel.VerticalBlackLine;
                                }
                            }
                            else
                            {
                                lineEnum = DrawDrugLineModel.VerticalLine;
                            }
                        }
                    }
                    else
                    {
                        if (nowTime.CompareTo(pageEndTime) > 0)
                        {
                            lineEnum = DrawDrugLineModel.VerticalLine;
                        }
                        else
                        {
                            lineEnum = DrawDrugLineModel.VerticalArrowLine;
                        }
                    }
                }
                else
                {
                    //不是最后一个
                    //如果当前Item比页面开始事件还小，说明不是这一页的线，跳过；当前item的Action为暂停，跳过
                    if (listDrugSpeed[i].BeginTime.CompareTo(pageStartTime) < 0 ||
                        listDrugSpeed[i].BeginTime.CompareTo(pageEndTime) > 0 ||
                        Constants.ACTION_PAUSE.Equals(listDrugSpeed[i - 1].Actions)) continue;
                    if (Constants.ACTION_PAUSE.Equals(listDrugSpeed[i].Actions))
                    {
                        lineEnum = DrawDrugLineModel.VerticalTwoLine;
                    }
                    else
                    {
                        lineEnum = DrawDrugLineModel.VerticalLine;
                    }

                    //前面已经判断是否是最后一个，所以这里一定有下一个
                    dsm2 = listDrugSpeed[i];
                }
                dsm1 = listDrugSpeed[i - 1];
                ctrl.PacuDisplay(lineEnum, patientDrug, dsm1, dsm2);
                _gridLine.Children.Add(ctrl);
                if (i == listDrugSpeed.Count() && !Constants.ACTION_FINISH.Equals(patientDrug.Status))
                {
                    DrugLinePacuItemMap.Add(listDrugSpeed[i - 1], ctrl);
                }
            }
        }

        public DateTime GetPageStartTime()
        {
            DateTime AnesRecordStartTime = DataManager.Instance.CurrentPatientLink.Status.AnesRecordStartTime;
            DateTime pageStartTime = AnesRecordStartTime.AddHours(4 * (CurrentPage - 1));
            return pageStartTime;
        }

        public DateTime GetPageEndTime()
        {
            DateTime AnesRecordStartTime = DataManager.Instance.CurrentPatientLink.Status.AnesRecordStartTime;
            DateTime pageEndTime = AnesRecordStartTime.AddHours(4 * (CurrentPage));
            return pageEndTime;
        }

        public DateTime GetPacuPageStartTime()
        {
            DateTime PacuStartTime = DataManager.Instance.CurrentPatientLink.Status.PacuStartTime;
            DateTime pageStartTime = PacuStartTime.AddHours(4 * (CurrentPage - 1));
            return pageStartTime;
        }

        public DateTime GetPacuPageEndTime()
        {
            DateTime PacuStartTime = DataManager.Instance.CurrentPatientLink.Status.PacuStartTime;
            DateTime pageEndTime = PacuStartTime.AddHours(4 * (CurrentPage));
            return pageEndTime;
        }
        #endregion
    }
}
